package collection.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class StreamApiDemo {
    public static void main(String[] args) {
        Random rand = new Random();
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<10;i++){
            list.add(rand.nextInt(100));
        }
        System.out.println(list);
        //扩大3倍
        List<Integer> list2 = list.stream()//将集合变为一个"流"
                                  .map(e->e*3)//添加map中间环节,将每个元素扩大3倍
                                  .collect(Collectors.toList());//终端环节,将流变为集合
        System.out.println(list2);
        //保留偶数
        List<Integer> list3 = list.stream().filter(e->e%2==0).collect(Collectors.toList());
        System.out.println(list3);
        //去重
        List<Integer> list4 = list.stream().distinct().collect(Collectors.toList());
        System.out.println(list4);
        //排序
        List<Integer> list5 = list.stream().sorted((e1,e2)->e2-e1).collect(Collectors.toList());
        System.out.println(list5);

        //合起来
        List<Integer> list6 = list.stream()
                .map(e->e*3)
                .filter(e->e%2==0)
                .distinct()
                .sorted((e1,e2)->e2-e1)
                .collect(Collectors.toList());
        System.out.println(list6);

    }
}
